package org.hj.modules.demo.test.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hj.common.api.vo.Result;
import org.hj.common.system.query.QueryGenerator;
import org.hj.modules.demo.test.entity.HjOrderCustomer;
import org.hj.modules.demo.test.entity.HjOrderMain;
import org.hj.modules.demo.test.entity.HjOrderTicket;
import org.hj.modules.demo.test.service.IHjOrderCustomerService;
import org.hj.modules.demo.test.service.IHjOrderMainService;
import org.hj.modules.demo.test.service.IHjOrderTicketService;
import org.hj.modules.demo.test.vo.HjOrderMainPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * 一对多示例（ERP TAB风格）
 */
@Slf4j
@RestController
@RequestMapping("/test/order")
public class HjOrderTabMainController {

    @Autowired
    private IHjOrderMainService hjOrderMainService;
    @Autowired
    private IHjOrderCustomerService hjOrderCustomerService;
    @Autowired
    private IHjOrderTicketService hjOrderTicketService;

    /**
     * 分页列表查询
     *
     * @param hjOrderMain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/orderList")
    public Result<?> respondePagedData(HjOrderMain hjOrderMain,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        QueryWrapper<HjOrderMain> queryWrapper = QueryGenerator.initQueryWrapper(hjOrderMain, req.getParameterMap());
        Page<HjOrderMain> page = new Page<HjOrderMain>(pageNo, pageSize);
        IPage<HjOrderMain> pageList = hjOrderMainService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param hjOrderMainPage
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HjOrderMainPage hjOrderMainPage) {
        HjOrderMain hjOrderMain = new HjOrderMain();
        BeanUtils.copyProperties(hjOrderMainPage, hjOrderMain);
        hjOrderMainService.save(hjOrderMain);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param hjOrderMainPage
     * @return
     */
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody HjOrderMainPage hjOrderMainPage) {
        HjOrderMain hjOrderMain = new HjOrderMain();
        BeanUtils.copyProperties(hjOrderMainPage, hjOrderMain);
        hjOrderMainService.updateById(hjOrderMain);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hjOrderMainService.delMain(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hjOrderMainService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HjOrderMain hjOrderMain = hjOrderMainService.getById(id);
        return Result.ok(hjOrderMain);
    }


    /**
     * 通过id查询
     *
     * @param hjOrderCustomer
     * @return
     */
    @GetMapping(value = "/listOrderCustomerByMainId")
    public Result<?> queryOrderCustomerListByMainId(HjOrderCustomer hjOrderCustomer,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<HjOrderCustomer> queryWrapper = QueryGenerator.initQueryWrapper(hjOrderCustomer, req.getParameterMap());
        Page<HjOrderCustomer> page = new Page<HjOrderCustomer>(pageNo, pageSize);
        IPage<HjOrderCustomer> pageList = hjOrderCustomerService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过id查询
     *
     * @param hjOrderTicket
     * @return
     */
    @GetMapping(value = "/listOrderTicketByMainId")
    public Result<?> queryOrderTicketListByMainId(HjOrderTicket hjOrderTicket,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<HjOrderTicket> queryWrapper = QueryGenerator.initQueryWrapper(hjOrderTicket, req.getParameterMap());
        Page<HjOrderTicket> page = new Page<HjOrderTicket>(pageNo, pageSize);
        IPage<HjOrderTicket> pageList = hjOrderTicketService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param hjOrderCustomer
     * @return
     */
    @PostMapping(value = "/addCustomer")
    public Result<?> addCustomer(@RequestBody HjOrderCustomer hjOrderCustomer) {
        hjOrderCustomerService.save(hjOrderCustomer);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param hjOrderCustomer
     * @return
     */
    @PutMapping("/editCustomer")
    public Result<?> editCustomer(@RequestBody HjOrderCustomer hjOrderCustomer) {
        hjOrderCustomerService.updateById(hjOrderCustomer);
        return Result.ok("添加成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteCustomer")
    public Result<?> deleteCustomer(@RequestParam(name = "id", required = true) String id) {
        hjOrderCustomerService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchCustomer")
    public Result<?> deleteBatchCustomer(@RequestParam(name = "ids", required = true) String ids) {
        this.hjOrderCustomerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 添加
     *
     * @param hjOrderTicket
     * @return
     */
    @PostMapping(value = "/addTicket")
    public Result<?> addTicket(@RequestBody HjOrderTicket hjOrderTicket) {
        hjOrderTicketService.save(hjOrderTicket);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param hjOrderTicket
     * @return
     */
    @PutMapping("/editTicket")
    public Result<?> editTicket(@RequestBody HjOrderTicket hjOrderTicket) {
        hjOrderTicketService.updateById(hjOrderTicket);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteTicket")
    public Result<?> deleteTicket(@RequestParam(name = "id", required = true) String id) {
        hjOrderTicketService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchTicket")
    public Result<?> deleteBatchTicket(@RequestParam(name = "ids", required = true) String ids) {
        this.hjOrderTicketService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

}